// pages/recommend/home/index.js
import utils from '../../../utils/util.js'

const app = getApp();

Component({
  data: {
    triggered: false,

    topNum: 0,
    floorstatus: false,

    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    WindowHeight: app.globalData.WindowHeight,
    TabCur: 1, //顶部选项卡索引
    TabList: [],

    num: 0,
    animationData: {}
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      let self = this;
      // self.hot = self.selectComponent("#hot")
      self.choiceness = self.selectComponent("#choiceness")
      self.like = self.selectComponent("#like")
      
      self.setData({
        TabList: [{
          index: 0,
          name: "关注"
        }, {
          index: 1,
          name: "行程"
        }
        , {
          index: 2,
          name: "印象"
        }
        ]
      })
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
      // backgroundAudioManager.pause();
    },
  },
  methods: {
    tabSelect: function(e) {
      let edata = e.currentTarget.dataset;
      if (this.data.TabCur == edata.tabindex){
        this.refresh(e);
      }else{
        this.setData({
          TabCur: edata.tabindex,
        })
      }
    },
    moveFinish(e) {
      this.setData({
        TabCur: e.detail.current,
      })
    },
    getData: function() {
      if (this.data.TabCur == 0){
        this.like.getData()
      } else if (this.data.TabCur == 1){
        this.choiceness.getData()
      }else{
        // this.hot.getData()
      }
    },
    onRefresh() {
      if (this._freshing) return
      this._freshing = true
      this.refresh()
      setTimeout(() => {
        this.setData({
          triggered: false,
        })
        this._freshing = false
      }, 3000)
    },
    
    toLowFun(e) {
      if (this.data.TabCur == 0) {
        this.like.addData()
      } else if (this.data.TabCur == 1) {
        this.choiceness.addData()
      } else {
        // this.hot.addData()
      }
    },
    // 获取滚动条当前位置
    scrolltoupper: function(e) {
      if (e.detail.scrollTop > 100) {
        this.setData({
          floorstatus: true
        });
      } else {
        this.setData({
          floorstatus: false
        });
      }
    },
    //回到顶部
    goTop: function(e) { // 一键回到顶部
      this.setData({
        topNum: this.data.topNum = 0
      });
    },
    //刷新
    refresh: function(e) {
      let that = this
      that.setData({
        topNum: 0
      });
      let id = setInterval(()=>{
        app.rotateAni(that, 'animationData', 'num')
      }, 500)
      setTimeout(() => {
        that.getData()
        clearInterval(id);
      }, 2000)
    },
  }
})